/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/08/19
*/
#include "VerticalLayout.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

VerticalLayout::VerticalLayout() : m_bCenter(false)
{}

VerticalLayout::~VerticalLayout()
{}

VerticalLayout* VerticalLayout::GetInterface()
{
    VerticalLayout* verticalLayout = new VerticalLayout;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[VerticalLayout] new verticalLayout : %d", verticalLayout);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("VerticalLayout", verticalLayout);
#endif
    return verticalLayout;
}

void VerticalLayout::Release()
{
    this->Clear();
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[VerticalLayout] delete verticalLayout : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

VerticalLayout* VerticalLayout::ToVerticalLayout(Control* ctrl)
{
    return dynamic_cast<VerticalLayout*>(ctrl);
}

bool VerticalLayout::IsCenter() const
{
    return m_bCenter;
}

void VerticalLayout::SetCenter(bool bCenter)
{
    m_bCenter = bCenter;
}

void VerticalLayout::AutoLayout()
{
    if (m_wnd->GetRoot() == this){
        m_rc.Reset(0, 0, m_wnd->GetWidth(), m_wnd->GetHeight());
    }
    Control::AutoLayout();

    if (m_items->size() == 0) return;

    Rect rc = m_rc;
    rc.x += m_rcInset.l;
    rc.y += m_rcInset.t;
    rc.w -= (m_rcInset.l + m_rcInset.r);
    rc.h -= (m_rcInset.t + m_rcInset.b);

    int fixed = 0;
    int autonum = 0;
    int total = 0;
    for (ItemMap::iterator it = m_items->begin(); it != m_items->end();  ++it){
        if ((*it) && (*it)->IsVisible() && !(*it)->IsFloat()){
            Size sz = (*it)->GetFixedSize();
            if (sz.h == 0) ++autonum;
            Rect1 padding = (*it)->GetPadding();
            fixed += sz.h + padding.t + padding.b;
            ++total;
        }
    }

    if (total > 1) fixed += (total - 1) * m_childPadding.h;

    int expand = 0;
    int expandoffset = 0;
    if (autonum > 0){
        expand = (rc.h - fixed) / autonum;
        expandoffset = (rc.h - fixed) - autonum * expand;
    }
    if (expand < 0){
        expand = 0;
        expandoffset = 0;
    }

    Rect rctmp;
    int posY = rc.y; int posX = 0;
    int w,h;
    for (ItemMap::iterator it = m_items->begin(); it != m_items->end();  ++it){
        if ((*it) && (*it)->IsVisible()){
            if ((*it)->IsFloat()){
                (*it)->SetRect(&rc);
                (*it)->AutoLayout();
            } else {
                Size sz = (*it)->GetFixedSize();
                if (sz.h == 0){
                    sz.h = expand + expandoffset;
                    expandoffset = 0;
                }
                Rect1 padding = (*it)->GetPadding();
                posX = (!m_bCenter || (sz.w == 0) || (sz.w >= rc.w)) ? rc.x : (rc.x + (rc.w - sz.w)/2);

                w = rc.w;
                h = sz.h + padding.t + padding.b;
                if (posX + w > rc.x + rc.w) w = rc.x + rc.w - posX;
                if (posY + h > rc.y + rc.w) h = rc.y + rc.h -posY;

                rctmp.Reset(posX, posY, w, h);

                (*it)->SetRect(&rctmp);
                (*it)->AutoLayout();

                posY += sz.h + m_childPadding.h + padding.t + padding.b;
            }
        }
    }
}

void VerticalLayout::SetAttribute(const char* name, const char* value)
{
    if (0 == strcmp(name, "center")){
        bool b = ParserBool(value, true);
        this->SetCenter(b);
    } else if (0 == strcmp(name, "childpadding")){
        ParserInt(value, &m_childPadding.h);
    } else {
        Container::SetAttribute(name, value);
    }
}

EDUI_NAMESPACE_END
